package com.xudow.app.dynamicstate_old.module.user;

import android.os.Bundle;
import android.support.annotation.NonNull;

import com.jude.beam.expansion.list.BeamListActivityPresenter;
import com.xudow.app.dynamicstate_old.data.DynamicModel;
import com.xudow.app.dynamicstate_old.domain.entity.AttentionUser;

/**
 * Created by ske on 2016/6/15.
 */
public class AttentionListPresenter extends BeamListActivityPresenter<AttentionListActivity, AttentionUser> {
    long lastTime;
    @Override
    protected void onCreate(@NonNull AttentionListActivity view, Bundle savedState) {
        super.onCreate(view, savedState);
        userId = getView().getIntent().getLongExtra("userId", -1);
        isFans = getView().getIntent().getBooleanExtra("isFans", false);
        onRefresh();
    }

    @Override
    protected void onCreateView(@NonNull AttentionListActivity view) {
        super.onCreateView(view);
        if (isFans)
            getView().setTitle();
    }

    long userId;
    boolean isFans;


    @Override
    public void onRefresh() {
        if (isFans)
            DynamicModel.getInstance().getFans(userId, 10, 0, 1)
                    .doOnNext(list -> {
                        if (list!=null&&list.size()>0)
                            lastTime = list.get(list.size()-1).getAttentionTime();
                    })
                    .doAfterTerminate(() -> getAdapter().notifyDataSetChanged())
                    .unsafeSubscribe(getRefreshSubscriber());
        else
            DynamicModel.getInstance().getAttentionUser(userId, 10, 0, 1)
                    .doOnNext(list -> {
                        if (list!=null&&list.size()>0)
                            lastTime = list.get(list.size()-1).getAttentionTime();
                    })
                    .doAfterTerminate(() -> getAdapter().notifyDataSetChanged())
                    .unsafeSubscribe(getRefreshSubscriber());
    }

    @Override
    public void onLoadMore() {
        if (isFans)
            DynamicModel.getInstance().getFans(userId, 10, lastTime, 2)
                    .doOnNext(list -> {
                        if (list!=null&&list.size()>0)
                            lastTime = list.get(list.size()-1).getAttentionTime();
                    })
                    .doAfterTerminate(() -> getAdapter().notifyDataSetChanged())
                    .unsafeSubscribe(getMoreSubscriber());
        else
            DynamicModel.getInstance().getAttentionUser(userId, 10, lastTime, 2)
                    .doOnNext(list -> {
                        if (list!=null&&list.size()>0)
                            lastTime = list.get(list.size()-1).getAttentionTime();
                    })
                    .doAfterTerminate(() -> getAdapter().notifyDataSetChanged())
                    .unsafeSubscribe(getMoreSubscriber());
    }
}
